package com.sapitest.imp;

import com.sapitest.keywords.HTTPClient;
import com.sapitest.keywords.JSONOperation;
import com.sapitest.utils.ConfigFile;
import com.sapitest.utils.bean.Response;

/**
 * 读取测试用例
 */
public class TestCases {
    private static final String URI = ConfigFile.getTestParameter("test_uri");
    private static final String GET_GOODS_LIST_PATH = ConfigFile.getTestParameter("get_goods_list_path");
    private static final String MY_SHOPPING_TROLLEY_PATH = ConfigFile.getTestParameter("my_shopping_trolley_path");

    /**
     * 处理请求体中deviceInfo复合参数，移除deviceInfo指定子字段，返回新的请求体
     * @param text  原始请求体
     * @param deviceInfo    deviceInfo复合参数
     * @param para  需要移除的子字段，如果剔除多个参数，格式为 key1,key2,...
     * @return  移除指定字段后的新字符串
     */
    private static String handleDeviceInfo(String text, String deviceInfo, String para){
        if (para.contains("imei")) {
            deviceInfo = JSONOperation.fluentRemoveAll(deviceInfo, "imei");
            text = JSONOperation.fluentPutJSONObject(text, "deviceInfo", deviceInfo);
        }
        if (para.contains("androidVersion")) {
            deviceInfo = JSONOperation.fluentRemoveAll(deviceInfo, "androidVersion");
            text = JSONOperation.fluentPutJSONObject(text, "deviceInfo", deviceInfo);
        }
        if (para.contains("manufacturer")) {
            deviceInfo = JSONOperation.fluentRemoveAll(deviceInfo, "manufacturer");
            text = JSONOperation.fluentPutJSONObject(text, "deviceInfo", deviceInfo);
        }
        if (para.contains("meid")) {
            deviceInfo = JSONOperation.fluentRemoveAll(deviceInfo, "meid");
            text = JSONOperation.fluentPutJSONObject(text, "deviceInfo", deviceInfo);
        }
        if (para.contains("model")) {
            deviceInfo = JSONOperation.fluentRemoveAll(deviceInfo, "model");
            text = JSONOperation.fluentPutJSONObject(text, "deviceInfo", deviceInfo);
        }
        return text;
    }

    /**
     * 接口：/v1/goodslist
     * 正常获取接口测试用例参数，并发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @return  自定义Response
     */
    public static Response testGetGoodsList(int caseID){
        String testUrl = URI + GET_GOODS_LIST_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String testBody = TestData.getGoodsListRequestData(caseID);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

    /**
     * 接口：/v1/goodslist
     * 获取接口测试用例并剔除指定的入参，并发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @param para  请求入参中需要剔除的key值，如果剔除多个参数，格式为 key1,key2,...
     * @return  自定义Response
     */
    public static Response testGetGoodsListWithoutPara(int caseID, String para){
        String testUrl = URI + GET_GOODS_LIST_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String text = TestData.getGoodsListRequestData(caseID);
        String deviceInfo = JSONOperation.getJSONObjectString(text, "deviceInfo");
        String testBody = JSONOperation.fluentRemoveAll(handleDeviceInfo(text, deviceInfo, para), para);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

    /**
     * 接口：/v1/goodslist
     * 获取接口测试用例并替换指定的入参值，发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @param key   请求入参key值
     * @param value 替换后的value值
     * @return  自定义Response
     */
    public static Response testGetGoodsListReplaceValue(int caseID, String key, Object value){
        String testUrl = URI + MY_SHOPPING_TROLLEY_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String testBody = JSONOperation.fluentPut(TestData.getGoodsListRequestData(caseID), key, value);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

    /**
     * 接口：/v1/myshoppingtrolley
     * 正常获取接口测试用例参数，并发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @return  自定义Response
     */
    public static Response testMyShoppingTrolley(int caseID){
        String testUrl = URI + MY_SHOPPING_TROLLEY_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String testBody = TestData.myShoppingTrolleyRequestData(caseID);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

    /**
     * 接口：/v1/myshoppingtrolley
     * 获取接口测试用例并剔除指定的入参，并发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @param para  请求入参中需要剔除的key值，如果剔除多个参数，格式为 key1,key2,...
     * @return  自定义Response
     */
    public static Response testMyShoppingTrolleyWithoutPara(int caseID, String para){
        String testUrl = URI + MY_SHOPPING_TROLLEY_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String text = TestData.myShoppingTrolleyRequestData(caseID);
        String deviceInfo = JSONOperation.getJSONObjectString(text, "deviceInfo");
        String testBody = JSONOperation.fluentRemoveAll(handleDeviceInfo(text, deviceInfo, para), para);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

    /**
     * 接口：/v1/myshoppingtrolley
     * 获取接口测试用例并替换指定的入参值，发送HTTP请求
     * @param caseID    数据库测试用例编号
     * @param key   请求入参key值
     * @param value 替换后的value值
     * @return  自定义Response
     */
    public static Response testMyShoppingTrolleyReplaceValue(int caseID, String key, Object value){
        String testUrl = URI + MY_SHOPPING_TROLLEY_PATH;
        String testHeader = "[{\"Content-type\":\"application/json\"},{\"Authorization\":\"XXX\"}]";
        String testBody = JSONOperation.fluentPut(TestData.myShoppingTrolleyRequestData(caseID), key, value);
        return HTTPClient.sendHttpRequest(testUrl,"post", testHeader, testBody);
    }

}
